//
//  LawViewController.m
//  PropertyManager
//
//  Created by TNG on 2018/7/17.
//  Copyright © 2018年 Tng. All rights reserved.
//

#import "LawViewController.h"

@interface LawViewController () <UITableViewDelegate,UITableViewDataSource>

@property (strong, nonatomic) UITableView * tableview;
@property (nonatomic,assign) int page;
@property (strong, nonatomic) NSMutableArray * dataArr;
@property (nonatomic,assign) BOOL isHeadFresh;

@end

@implementation LawViewController

static NSString * LawTableViewCellID = @"LawTableViewCell";

- (void)viewDidLoad {
    [super viewDidLoad];
    self.page = 1;
    self.title = @"法制宣传";
    [self creatTableview];
    [self registCell];
    [self requestData];
}

-(NSMutableArray *)dataArr
{
    if(_dataArr == nil)
    {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}

-(void)requestData
{
    WeakSelf;
    NSString * path = [NSString stringWithFormat:@"/rest/gbLawController/getLawList?id=&title=&bpmStatus=&createName=&page=%d&size=10",self.page];
    [[HTTPClient shareClient] requestWithPath:path AndReturnBlock:^(NSDictionary *dic) {
        if (weakSelf.isHeadFresh) {
            [weakSelf.dataArr removeAllObjects];
        }
        NSArray * dataArr = dic[@"data"][@"list"];
        for (NSDictionary * dics in dataArr) {
            LawModel * model = [LawModel modelWithDic:dics];
            [weakSelf.dataArr addObject:model];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [weakSelf.tableview reloadData];
        });
    } AndFailMessage:^(NSString *message, NSString *errorCode) {
        [SVProgressHUD ShowErrorMessage:message];
    }];
}

-(void)registCell
{
    [self.tableview registerClass:[LawTableViewCell class] forCellReuseIdentifier:LawTableViewCellID];
}

-(void)creatTableview{
    if (self.tableview == nil) {
        UITableView * tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, kStatusAndNavBarH, WIDTH, HEIGHT - kStatusAndNavBarH - kHomeLineH) style:UITableViewStyleGrouped];
        tableview.delegate = self;
        tableview.dataSource = self;
        if (@available(iOS 11.0, *)) {
            tableview.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAutomatic;
        }
        self.tableview = tableview;
        [self.view addSubview:self.tableview];
    }
    // 下拉刷新
    WeakSelf;
    self.tableview.mj_header= [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakSelf.isHeadFresh = YES;
        weakSelf.page = 1;
        [weakSelf requestData];
        [weakSelf.tableview.mj_header endRefreshing];
    }];
    // 设置自动切换透明度(在导航栏下面自动隐藏)
    self.tableview.mj_header.automaticallyChangeAlpha = YES;
    
    self.tableview.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        // 结束刷新
        weakSelf.isHeadFresh = NO;
        weakSelf.page += 1;
        [weakSelf requestData];
        [weakSelf.tableview.mj_footer endRefreshing];
    }];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 168;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return [UIView new];
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
//    SDCycleScrollView * sc = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, WIDTH, 168) delegate:nil placeholderImage:nil];
//    sc.autoScrollTimeInterval = 3;
//    sc.backgroundColor = SFMRandColor;
//    sc.imageURLStringsGroup = _logoArr;
//    return sc;
    UIImageView * img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 168)];
    img.image = [UIImage imageNamed:@"lawBannerImg"];
    img.contentMode = UIViewContentModeScaleAspectFill;
    img.clipsToBounds = YES;
    return img;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 87;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LawTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:LawTableViewCellID];
    if(self.dataArr.count)
    {
        LawModel * model = [self.dataArr objectAtIndex:indexPath.row];
        [cell sendLawModel:model];
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    HtmlWebViewController * ctr = [[HtmlWebViewController alloc] init];
    LawModel * model = [self.dataArr objectAtIndex:indexPath.row];
    ctr.url = model.lawID;
    [self.navigationController pushViewController:ctr animated:YES];
}

-(BOOL)hidesBottomBarWhenPushed
{
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
